<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" :badge-data="badgeData" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        stripe
        size="large"
        border
      >
        <template #id="{ row }">
          {{ row.id }}
          <br />
          {{ row.cate }}
        </template>

        <template #plate_number="{ row }">
          <span class="blue pointer" @click="openDetail(row)">
            {{ row.plate_number }}
          </span>
        </template>

        <template #remark="{ row }">
          <span class="orange">{{ row.remark }}</span>
        </template>

        <template #list="{ row }">
          <el-link
            type="danger"
            :underline="false"
            @click="openCreateffer(row, '商业险')"
          >
            查看({{ row.offer_times }})
          </el-link>
        </template>

        <template #actionOffer="{ row }">
          <el-button-group>
            <el-button type="success" size="small" @click="openCreate(row)">
              添加报价单
            </el-button>
            <el-button type="primary" size="small" @click="toChoose(row)">
              报价完成
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 报价 -->
    <create
      v-model="showCreate"
      :vehicle-id="vehicleId"
      :plan-id="planId"
      :plate-number="plateNumber"
      @done="reload"
    />
    <!-- <offer-b
      v-model="showOb"
      :data="current"
      :plan-id="planId"
      @done="reload"
    /> -->
    <!-- 报价列表 -->
    <offer-list
      v-model="showOffer"
      :data="current"
      :offer-type="offerType"
      action="offer"
    />
    <!-- 车辆详情 -->
    <detail v-model="showDetail" :data="current" />
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import Detail from '@/views/vehicle/components/detail.vue';
  import Search from './components/search.vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessageBox } from 'element-plus/es';
  import { pageInsurancePlanList, setPlanToChoose } from '@/api/company';
  import Create from '@/views/insurance/components/create.vue';
  import OfferList from './components/offer-list.vue';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [
      // {
      //   slot: 'id',
      //   label: '类型',
      //   width: 95,
      //   align: 'center'
      // },
      // {
      //   slot: 'vehicle',
      //   label: '车辆信息',
      //   minWidth: 200,
      //   align: 'center'
      // },
      {
        prop: 'id',
        label: 'ID',
        width: 80
      },

      {
        slot: 'plate_number',
        label: '车牌号',
        width: 120
      },
      {
        prop: 'company',
        label: '所属车队',
        width: 200
      },
      {
        slot: 'list',
        label: '报价列表',
        width: 120,
        align: 'center'
      },
      {
        slot: 'remark',
        label: '报价要求'
      },
      {
        width: 90,
        align: 'center',
        prop: 'create_user',
        label: '申请人'
      },
      {
        width: 150,
        prop: 'create_time',
        label: '申请时间'
      }
      // {
      //   slot: 'remarkC',
      //   label: '交强险',
      //   width: 110,
      //   align: 'center'
      // },
      // {
      //   slot: 'remarkB',
      //   label: '商业险',
      //   width: 110,
      //   align: 'center'
      // },
      // {
      //   width: 110,
      //   slot: 'cp',
      //   label: '超赔',
      //   align: 'center'
      // },
      // {
      //   width: 110,
      //   slot: 'hy',
      //   label: '货运',
      //   align: 'center'
      // },
      // {
      //   width: 110,
      //   slot: 'az',
      //   label: '安责',
      //   align: 'center'
      // },
      // {
      //   width: 110,
      //   slot: 'cy',
      //   label: '承运',
      //   align: 'center'
      // },
      // {
      //   width: 110,
      //   slot: 'jy',
      //   label: '驾意',
      //   align: 'center'
      // }
    ];

    cols.push({
      columnKey: 'actionOffer',
      label: '操作',
      slot: 'actionOffer',
      fixed: 'right',
      width: 200,
      align: 'center'
    });

    return cols;
  });

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 搜索框里面的小红点数据 */
  const badgeData = ref({});

  /** 表格数据源 */
  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pageInsurancePlanList({
      ...where,
      ...orders,
      page,
      limit,
      status: 0
    });

    badgeData.value = result.badgeData;

    return result;
  };

  /** 默认搜索条件 */
  const chooseStatus = ref(1);
  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
    console.log('是这里出错？', where);
    chooseStatus.value = where ? where.choose_status : 1;
  };

  /** 报价 */
  const planId = ref(0);
  const vehicleId = ref(0);
  const plateNumber = ref('');
  const showCreate = ref(false);
  const openCreate = (row) => {
    planId.value = row.id;
    vehicleId.value = row.vehicle_id;
    plateNumber.value = row.plate_number;
    showCreate.value = true;
  };
  // const showOb = ref(false);
  // const openB = (row) => {
  //   planId.value = row.id;
  //   current.value = row ?? null;
  //   showOb.value = true;
  // };

  /** 报价 */
  const offerType = ref('');
  const showOffer = ref(false);
  const openCreateffer = (row, type) => {
    current.value = row;
    planId.value = row.id;
    showOffer.value = true;
    offerType.value = type;
  };

  /** */
  /** 车辆详情 */
  const showDetail = ref(false);
  const openDetail = (row) => {
    current.value = row ?? null;
    showDetail.value = true;
  };

  // 提交报价
  const toChoose = (row) => {
    ElMessageBox.confirm(
      '确定将' + row.plate_number + row.remark + '的报价申请标记为已报价吗？',
      '系统提示',
      {
        type: 'warning',
        draggable: true
      }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        setPlanToChoose({ id: row.id, trade_id: row.trade_id })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
            reload();
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };
</script>
